package com.yeyks.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.yeyks.other.dal.domain.ImportCityDataConfig;
import com.yeyks.other.service.ImportCityDataConfigService;
import com.yeyks.search.constants.EsIndexConstant;
import com.yeyks.search.service.hotel.EsImportHotelService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Es导入本地数据库酒店
 *
 * @author: dagny
 * @date: 2019/9/29 0029 12:09
 */
@RestController
@RequestMapping("/import")
@Slf4j
public class EsImportHotelController {


    @Autowired
    private EsImportHotelService esImportHotelService;

    @Autowired
    private ImportCityDataConfigService importCityDataConfigService;

    @ApiOperation("导入酒店信息")
    @PostMapping("/hotel")
    public int importHotel(@RequestParam(value = "hotelId", required = true) Integer hotelId,

                           @RequestParam(value = "indexName", defaultValue = "") String indexName){

        esImportHotelService.importHotelByHotelId(hotelId, indexName);

        return 0;
    }


    @ApiOperation("批量导入酒店信息")
    @PostMapping("/hotel/list")
    public int importHotel(@RequestParam(value = "indexName", defaultValue = "") String indexName){

        esImportHotelService.importHotelByPage(indexName);
        return 0;
    }



    @ApiOperation("导入CnBooking酒店信息")
    @PostMapping("/cb/hotel")
    public int importHotel(@RequestParam(value = "hotelId") Integer hotelId,
                           @RequestParam(value = "indexName", required = false) String indexName,
                           @RequestParam(value = "countryId", required = false) String countryId,
                           @RequestParam(value = "provinceId", defaultValue = "", required = false) String provinceId,
                           @RequestParam(value = "cityId", defaultValue = "", required = false) String cityId){


        if (null == indexName) {
            indexName = EsIndexConstant.INDEX_HOTEL;
        }
        if (null == countryId) {
            countryId = "0001";
        }
        esImportHotelService.importCnBookingHotelByHotelId(hotelId, indexName, countryId,
                provinceId, cityId);
        return 0;
    }

    @ApiOperation("批量导入CnBooking酒店信息")
    @PostMapping("/cb/hotel/list")
    public int importHotelList(
                           @RequestParam(value = "indexName", defaultValue = "") String indexName,
                           @RequestParam(value = "countryId", defaultValue = "0001") String countryId,
                           @RequestParam(value = "provinceId", defaultValue = "", required = false) String provinceId,
                           @RequestParam(value = "cityId", defaultValue = "", required = false) String cityId,
                           @RequestParam(value = "pageSize", defaultValue = "10")Integer pageSize,
                           @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo){

        esImportHotelService.importCnBookingHotelByPage(indexName, countryId, provinceId, cityId, pageSize, pageNo);
        return 0;
    }


    /**
     * 循环导入省的数据信息
     * @return 结果
     */
    @ApiOperation("批量城市导入CnBooking酒店信息")
    @PostMapping("/cb/hotel/province")
    public int importAllProvinceList(@RequestParam(value = "indexName", defaultValue = "") String indexName,
                                     @RequestParam(value = "countryId", defaultValue = "0001") String countryId,
                                     @RequestParam(value = "provinceId", defaultValue = "", required = false) String provinceId,
                                     @RequestParam(value = "cityIdList", defaultValue = "", required = false) String cityIdList){

        String[] split = cityIdList.split(",");
        if(split.length > 0){
            for(String cityId : split){
                log.info("=====开始导入城市ID为【{}】的数据====",cityId);
                importHotelList(indexName, countryId, provinceId, cityId, 20,1);
                log.info("=====结束导入城市ID为【{}】的数据====",cityId);
            }
        }
        return 0;
    }

    @ApiOperation("Map中的第三批剩下的城市酒店信息")
    @PostMapping("/cb/hotel3/map/list")
    public int importMap2List(@RequestParam(value = "indexName", defaultValue = "") String indexName){

        long startTime = System.currentTimeMillis();
        Map<String, String> map = Maps.newHashMap();

        //
        map.put("2000","2002,2006,2021,2014,2013,2027,2016,2028");
        Set<String> strings = map.keySet();
        for (String provinceId : strings){
            String cityList = map.get(provinceId);
            if(!StringUtils.isEmpty(cityList)){
                String[] split = cityList.split(",");
                if(split.length > 0){
                    for(String cityId : split){
                        log.info("=====开始导入城市ID为【{}】的数据====",cityId);
                        /*esImportHotelService.importCnBookingHotelByPage(indexName,"0001",
                                provinceId, cityId, 10, 1);*/
                        log.info("=====结束导入城市ID为【{}】的数据====",cityId);
                    }
                }
            }
        }
        long endTime = System.currentTimeMillis();
        log.info("酒店数据录入完成，消耗总时长【{}】毫秒",
                (endTime - startTime));

        return 0;
    }


    /**
     * 批量更新房态的底价和销售价
     * @return 结果
     */
    @ApiOperation("批量更新房态的底价和销售价")
    @PostMapping("/cb/calendar/price")
    public int updateCalendarPrice(
                                   @RequestParam(value = "indexName", defaultValue = "") String indexName,
                                   @RequestParam(value = "highPercent", defaultValue = "1") String highPercent,
                                   @RequestParam(value = "lowPercent", defaultValue = "1") String lowPercent,
                                   @RequestParam(value = "calendarId", defaultValue = "", required = false) String calendarId,
                                   @RequestParam(value = "pageSize", defaultValue = "100")Integer pageSize,
                                   @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo){


        esImportHotelService.updateCalendarPrice(indexName, highPercent, lowPercent, calendarId, pageSize, pageNo);
        return 0;
    }

    @ApiOperation("批量更新房间图片")
    @PostMapping("/cb/room/image")
    public int updateRoomImageIndexHotel(@RequestParam(value = "indexName", defaultValue = "") String indexName,
                                         @RequestParam(value = "sourceType", defaultValue = "cn", required = false) String sourceType,
                                         @RequestParam(value = "pageSize", defaultValue = "100")Integer pageSize,
                                         @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo){

        esImportHotelService.updateRoomImageIndexHotel(indexName, sourceType, pageSize, pageNo);
        return 0;
    }

    @ApiOperation("更新房间图片")
    @PostMapping("/update/cb/room/image")
    public int updateRoomImage(@RequestParam(value = "sourceId") String sourceId,
                                         @RequestParam(value = "imgJson") String imgJson){

        Boolean aBoolean = esImportHotelService.updateRoomImage(sourceId, imgJson);
        if (aBoolean){
            return 0;
        }
        return -1;
    }


    @ApiOperation("更新酒店图片")
    @PostMapping("/update/cb/hotel/image")
    public int updateHotelImage(@RequestParam(value = "sourceId") String sourceId,
                                         @RequestParam(value = "imgJson") String imgJson){

        Boolean aBoolean = esImportHotelService.updateHotelImage(sourceId, imgJson);
        if (aBoolean){
            return 0;
        }
        return -1;
    }

    @ApiOperation("定时清除房态数据信息")
    @PostMapping("/del/cb/calendar")
    public Boolean deleteEsCalendar(@RequestParam(value = "indexName", defaultValue = "") String indexName,
                                    @RequestParam(value = "sourceType", defaultValue = "cn", required = false) String sourceType,
                                    @RequestParam(value = "pageSize", defaultValue = "100")Integer pageSize,
                                    @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo){

        return esImportHotelService.delOverTimeCalendarData(indexName,sourceType, pageSize, pageNo);
    }

    @ApiOperation("导入ID大于等于某个值的城市信息")
    @PostMapping("/import/hotel/by_id")
    public int importHotelByCreateTime(@RequestParam(value = "indexName", defaultValue = "") String indexName,
                                       @RequestParam(value = "pageSize", defaultValue = "100")Integer pageSize,
                                       @RequestParam(value = "pageNo", defaultValue = "1")Integer pageNo,
                                       @RequestParam(value = "id", required = true) Integer id){
        //查询导入城市的集合
        List<ImportCityDataConfig> list = importCityDataConfigService.list(new QueryWrapper<ImportCityDataConfig>()
        .ge(ImportCityDataConfig.ID, id));
        if(!CollectionUtils.isEmpty(list)){
            for (ImportCityDataConfig importCityDataConfig : list){
                //执行数据导入
                esImportHotelService.importCnBookingHotelByPage(indexName,"0001",
                        importCityDataConfig.getProvinceId(), importCityDataConfig.getCityId(),
                        pageSize,pageNo);

                log.info(importCityDataConfig.getProvinceName()+"的：" + importCityDataConfig.getCityName() + "导入成功！");
            }
            return 0;
        }
        return -1;
    }

}
